package com.zjy.controller;

import com.zjy.bean.Address;
import com.zjy.dao.AddressMapper;
import com.zjy.dto.ApiResult;
import com.zjy.service.AddressService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/5/18.
 */
@Controller
@CrossOrigin
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @Resource
    private AddressMapper addressMapper;

    /**
     * 新增收货地址
     * @param address
     */
    @RequestMapping(value = "/addAddress", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addAddress(@RequestBody Address address){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(address.getAddress()) || ObjectUtils.isNullOrEmpty(address.getPhone())){
            return result.error(CLIENT_ERROR,"地址和手机号不能为空！");
        }else {
            try{
                addressService.addAddress(address);
                result.setMsg("新增地址成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,"新增地址失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 删除收货地址
     * @param address
     */
    @RequestMapping(value = "/deleteAddressById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteAddressById(@RequestBody Address address){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(address.getId())){
            return result.error(CLIENT_ERROR,"id不能为空！");
        }else {
            try{
                int count = addressService.deleteAddressById(address.getId());
                if(count == 1){
                    result.setMsg("删除收货地址成功！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"删除失败，没找到此id对应的收货地址");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"删除收货地址失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 修改收货地址
     * @param address
     */
    @RequestMapping(value = "/updateAddressById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateAddressById(@RequestBody Address address){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(address.getId()) || ObjectUtils.isNullOrEmpty(address.getAddress()) ||
                ObjectUtils.isNullOrEmpty(address.getPhone())){
            return result.error(CLIENT_ERROR,"id、地址和手机号都不能为空！");
        }else {
            try{
                int count = addressMapper.updateByPrimaryKeySelective(address);
                if(count == 1){
                    result.setMsg("修改收货地址成功！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"修改失败，没找到此id对应的收货地址");
                }

            }catch (Exception e){
                return result.error(SERVER_ERROR,"修改收货地址失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据id查询收货地址
     * @param address
     */
    @RequestMapping(value = "/selectAddressById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectAddressById(@RequestBody Address address){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(address.getId())){
            return result.error(CLIENT_ERROR,"id不能为空！");
        }else {
            try{
                Address address1 = addressMapper.selectAddressById(address.getId());
                if(address1 != null){
                    result.setData(address1);
                    result.setMsg("查询收货地址成功！");
                }else {
                    result.error(CLIENT_ERROR,"查询失败，没找到此id对应的收货地址");
                }

            }catch (Exception e){
                return result.error(SERVER_ERROR,"查询收货地址失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据userId查询用户收货地址列表
     * @param address
     */
    @RequestMapping(value = "/selectAddressByUserId", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectAddressByUserId(@RequestBody Address address){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(address.getUserId())){
            return result.error(CLIENT_ERROR,"userId不能为空！");
        }else {
            try{
                List<Address> addressList = addressMapper.selectAddressByUserId(address.getUserId());
                if(addressList != null){
                    result.setData(addressList);
                    result.setMsg("查询收货地址列表成功！");
                }else {
                    result.error(CLIENT_ERROR,"查询失败，没找到此userId对应的收货地址列表");
                }

            }catch (Exception e){
                return result.error(SERVER_ERROR,"查询收货地址失败，原因："+ e.getMessage());
            }
        }
        return result;
    }
}
